﻿using System.Web.Mvc;
using INSE6260.OnlineBanking.Model.Payee;
using INSE6260.OnlineBanking.Web.MVC.Helpers;

namespace INSE6260.OnlineBanking.Web.MVC.Controllers
{
    public class PayeeInfoController : Controller
    {
        private readonly IPayeeRepository _payeeRepository;
        private readonly IPayeeNameRepository _payeeNameRepository;
        public PayeeInfoController(IPayeeRepository payeeRepository, IPayeeNameRepository payeeNameRepository)
        {
            _payeeRepository = payeeRepository;
            _payeeNameRepository = payeeNameRepository;
        }

        #region Index
        //
        // GET: /PayeeInfo/

        public ViewResult Index()
        {
            var payees = _payeeRepository.GetClientPayee(UserProvider.ActiveClient.ClientId);
            return View(payees);
        }
        #endregion

        #region Create
        //
        // GET: /PayeeInfo/Create

        public ActionResult Create()
        {
            ViewBag.ClientId = UserProvider.ActiveClient.ClientId;
            ViewBag.PayeeNameList = _payeeNameRepository.GetAllPayeeName();
            return View();
        }
        [HttpGet]
        public ActionResult GetInstruction (int payeenameId)
        {
            var payeeName = _payeeNameRepository.GetPayeeNameById(payeenameId);
            return Json(payeeName.Instruction, JsonRequestBehavior.AllowGet);
        }

        //
        // POST: /PayeeInfo/Create

        [HttpPost]
        public ActionResult Create(Payee payee)
        {
            if (ModelState.IsValid)
            {
                payee.ClientId = UserProvider.ActiveClient.ClientId;
                _payeeRepository.CreatePayee(payee);
                return RedirectToAction("Index");
            }

            ViewBag.ClientId = UserProvider.ActiveClient.ClientId;
            ViewBag.PayeeNameId = payee.PayeeName.Id;
            return View(payee);
        }
        #endregion

        #region Edit
        //
        // GET: /PayeeInfo/Edit/5

        public ActionResult Edit(int id)
        {
            var payee = _payeeRepository.GetPayeeById(id);

            ViewBag.ClientId = UserProvider.ActiveClient.ClientId;
            ViewBag.PayeeNameId = payee.PayeeName.Id;
            return View(payee);
        }

        //
        // POST: /PayeeInfo/Edit/5

        [HttpPost]
        public ActionResult Edit(Payee payee)
        {
            if (ModelState.IsValid)
            {
                var dbPayee = _payeeRepository.GetPayeeById(payee.Id);
                dbPayee.NickName = payee.NickName;
                dbPayee.AccountNo = payee.AccountNo;
                _payeeRepository.EditPayee(dbPayee);
                return RedirectToAction("Index");
            }
            ViewBag.ClientId = UserProvider.ActiveClient.ClientId;
            ViewBag.PayeeNameId = payee.PayeeName.Id;
            return View(payee);
        }
        #endregion

        #region Delete
        //
        // GET: /PayeeInfo/Delete/5

        public ActionResult Delete(int id)
        {
            var payee = _payeeRepository.GetPayeeById(id);
            return View(payee);
        }

        //
        // POST: /PayeeInfo/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var payee = _payeeRepository.GetPayeeById(id);
            _payeeRepository.DeletePayee(payee);
            return RedirectToAction("Index");
        }
        #endregion
    }
}